package com.dz.lm.service.userorderlog.impl;

import com.dz.lm.common.dto.UserOrderLogDTO;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.dao.userorder.read.UserOrderDao;
import com.dz.lm.dao.userorderlog.read.UserOrderLogDao;
import com.dz.lm.domain.userorderlog.UserOrderLog;
import com.dz.lm.domain.userorderlog.UserOrderLogExample;
import com.dz.lm.service.userorderlog.UserOrderLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserOrderLogServiceImpl implements UserOrderLogService {
    @Resource(name = "readUserOrderLogDao")
    private UserOrderLogDao readUserOrderLogDao;
    @Resource(name = "writeUserOrderLogDao")
    private com.dz.lm.dao.userorderlog.write.UserOrderLogDao writeUserOrderLogDao;
    @Override
    public int add(String orderId, int applyId, String status, int userId) throws ServiceException {
        UserOrderLog log = new UserOrderLog();
        log.setOrderId(orderId);
        log.setApplyId(applyId);
        log.setStatus(status);
        log.setUserId(userId);
        return writeUserOrderLogDao.insertSelective(log);
    }

    @Override
    public List<UserOrderLogDTO> query(String orderId) throws ServiceException {
        List<UserOrderLogDTO> dtos = new ArrayList();
        UserOrderLogExample ex = new UserOrderLogExample();
        UserOrderLogExample.Criteria cr = ex.createCriteria();
        cr.andOrderIdEqualTo(orderId);
        List<UserOrderLog> logs = readUserOrderLogDao.selectByExample(ex);
        if(logs != null && logs.size() > 0){
            for (UserOrderLog log : logs) {
                UserOrderLogDTO dto = new UserOrderLogDTO();
                BeanUtils.copyProperties(log,dto);
                dtos.add(dto);
            }
        }
        return dtos;
    }
}
